<template>
  <el-scrollbar class="scrollbar-container">
    <el-form style="padding-right: 16px;" ref="formRef" :model="formValue" :rules="rules" label-position="top">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formValue.name" placeholder="请输入姓名" clearable></el-input>
      </el-form-item>

      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formValue.sex">
          <el-radio label="-1">未知</el-radio>
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="兴趣爱好" prop="hobbies">
        <el-input v-model="formValue.hobbies" placeholder="请输入兴趣爱好" clearable></el-input>
      </el-form-item>

      <el-form-item label="特长" prop="specialty">
        <el-select v-model="formValue.specialty" placeholder="请选择兴趣爱好" clearable>
          <el-option label="篮球" value="basketball"></el-option>
          <el-option label="足球" value="football"></el-option>
          <el-option label="乒乓球" value="pingPong"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-scrollbar>
</template>

<script>
export default {
  name: 'dialog-1',
  props: {
    form: {
      type: Object,
    },
  },
  data() {
    return {
      formValue: {
        name: '',
        sex: '',
        hobbies: '',
        specialty: '',
      },
      rules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        sex: [{ required: true, message: '性别不能为空', trigger: 'change' }],
      },
    };
  },
  watch: {
    form: {
      handler(val) {
        if (val) {
          Object.keys(this.formValue).forEach(key => {
            this.formValue[key] = val[key];
          });
        }
      },
      immediate: true,
    },
  },
  mounted() {
  },
  methods: {
    /**
     * 取消操作
     * @param cb
     */
    handleCancel(cb) {
      typeof cb === 'function' && cb.call(this, false);
    },

    /**
     * 保存操作
     * @param cb
     */
    handleSave(cb) {
      this.$refs.formRef.validate(valid => {
        if (!valid) return;

        const data = {
          ...this.formValue,
        };
        if (this.form.id) {
          data.id = this.form.id;
        }
        this.$message({ type: 'success', message: '保存成功' });
        typeof cb === 'function' && cb.call(this, data);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.scrollbar-container {
  width: 100%;
  height: 300px;

  ::v-deep .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}
</style>
